package neo4j.learn.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import RelationshipType.RelationType;


/**
 * 做neo4j数据库的操作学习
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/")
public class Test1Controller {
	
	SpringCypherRestGraphDatabase neo4jDb = new SpringCypherRestGraphDatabase("http://localhost:7474/db/data/", "neo4j", "123456");
	
	
	@RequestMapping("/abc")
	public String toAdminlogout(HttpServletRequest request){
		System.out.println("here is abc controller！！");
		
		
		List<Node> nodes = findNodes("ACCOUNT");
		for(Node node:nodes){
			System.out.println(node.getPropertyKeys().toString());
		}
		System.out.println("*******");
		//createNode();
		System.out.println("*******");
		findNode1();
		findNode2();
		delNode();
		
		
		/*ResourceIterator<Node> orgNodes = neo4jDb.findNodes(Label);
		while(orgNodes.hasNext()){
			Node node = orgNodes.next();
			String name = node.getProperty("name").toString();
			System.out.println(node.toString()+"   "+name);
		}*/
		
		/*Collection<String> list = neo4jDb.getAllLabelNames();
		Iterator<String>  lit = list.iterator();
		while(lit.hasNext()){
			System.out.println(lit.next());
		}*/
		
		
		return "abc";
	}
	
	//按照标签名查找节点
	public List<Node> findNodes(String labelName) {
		List<Node> list = new ArrayList<Node>();
		Label label = DynamicLabel.label(labelName);
		ResourceIterator<Node> nodes = neo4jDb.findNodes(label);
		while (nodes.hasNext()) {
			list.add(nodes.next());
		}
		return list;
	}
	
	//创建一个节点
	public Boolean createNode(){
		Node node = neo4jDb.createNode();
		node.setProperty("name", "aaa");
		node.setProperty("year", "2016");
		Label Label = DynamicLabel.label("PERSON");
		node.addLabel(Label);
		Node node2 = neo4jDb.createNode();
		node2.setProperty("name", "bbb");
		node2.setProperty("year", "2016");
		node2.addLabel(Label);
		Relationship rs = node.createRelationshipTo(node2,RelationType.FRIENDS);
		rs.setProperty("remark", "老好人");
		rs.setProperty("group", "我的好友");
		return true;
	}
	
	
	//找到一个节点
	public Boolean findNode1(){
		Label Label = DynamicLabel.label("QQ");
		Node node = neo4jDb.findNode(Label, "uin", "123456789");
		String name = node.getProperty("location").toString();
		System.out.println(node.toString()+"   "+name);
		return true;
	}
	
	//找到一个节点
	public Boolean findNode2(){
		Node node = neo4jDb.getNodeById(50);
		String name = node.getProperty("location").toString();
		System.out.println(node.toString()+"   "+name);
		return true;
	}
	
	//删除节点
	public Boolean delNode(){
		Node node = neo4jDb.getNodeById(5);
		//String name = node.getProperty("name").toString();
		//System.out.println(name);
		 Iterator<Relationship> rl = node.getRelationships(Direction.INCOMING).iterator();
		 while(rl.hasNext()){
			 neo4jDb.remove(rl.next());
			 System.out.println(rl.toString());
		 }
		neo4jDb.remove(node);
		
		return true;
	}

}
